import { getSetting, chooseAddress, openSetting, showModal, showToast } from "../../utils/Wxasync"
Page({
    data: {
        address: {},
        cart: [],
        allChecked: false,
        totalPrice: 0,
        totalNum: 0
    },
    onShow() {
        const address = wx.getStorageSync("address");
        const cart = wx.getStorageSync("cart") || [];
        this.setData({
            address
        })
        //调用自定义的方法setcart去更新购物车的数据
        this.setCart(cart)
    },
    /// 获取收货地址
    async handleChooseAddress() {
        try {
            // 1 获取权限状态
            const res1 = await getSetting()
            //表示看看用户有没有获取收货地址的权限;
            const scopeAddress = res1.authSetting['scope.address']
            if (scopeAddress === false) {
                //打开权限设置的页面; ---让用户重新设置权限;
                await openSetting()
            }

            // 调用收货地址的api
            let address = await chooseAddress()
            console.log(address)
            address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;

            //存到缓存中
            wx.setStorageSync("address", address);

        } catch (error) {
            console.log('你干哈呢')
        }
    },
    setCart(cart) {
        let allChecked = true
        let totalPrice = 0
        let totalNum = 0
        cart.forEach(v => {
            if (v.checked) {
                totalPrice += v.num * v.goods_price
                totalNum += v.num
            } else {
                allChecked = false
            }
        })
        allChecked = cart.length != 0 ? allChecked : false
        this.setData({
            cart,
            totalPrice,
            totalNum,
            allChecked
        })
    }, 
    /// 选中商品变化
    handleChange(e) {
        const goods_id = e.currentTarget.dataset.id
        let { cart } = this.data
        let index = cart.findIndex(v => v.goods_id === goods_id)
        cart[index].checked = !cart[index].checked /// 状态取反
        this.setCart(cart)
    },
    /// 全选事件
    handeItemChangeAll() {
        let { cart, allChecked } = this.data
        allChecked = !allChecked
        cart.forEach(v => v.checked = allChecked)
        this.setCart(cart)
    },
    /// 商品数量加减 删除
    async handleEditNum(e) {
        let { operation, id } = e.currentTarget.dataset
        let { cart } = this.data
        const index = cart.findIndex(v => v.goods_id === id)
        if (cart[index].num === 1 && operation === -1) {
            const res = await showModal({
                content: '您是否要删除该商品'
            })
            if (res.confirm) {
                cart.splice(index, 1)
                this.setCart(cart)
            }
        } else {
            cart[index].num += operation
            this.setCart(cart)
        }
    },
    /// 商品支付
    async handlePayGoods() {
        //判断收货地址
        const { address, totalNum } = this.data
        if (!address.userName) {
            await showToast({
                title: '请选择收货地址'
            })
            return
        }
        if (totalNum === 0) {
            await showToast({
                title: '请选择选购商品'
            })
            return
        }
        wx.navigateTo({
            url: "/pages/pay/pay"
        })
    }
})